#include "head.h"

void do_set_info(int sockfd,MSG *pbuf)
{
	pbuf->head[0] = 's';
	pbuf->head[1] = 't';
	pbuf->head[2] = ':';
//	pbuf->type = int;
	pbuf->snum = 1;
	pbuf->temp[0] = (uint8_t)(20 + random(20));
	pbuf->hum[0] = (uint8_t)(30 + random(30));
	pbuf->x = (uint8_t)(10 + random(10));
	pbuf->y = (uint8_t)(20 + random(20));
	pbuf->z = (uint8_t)(30 + random(30));
	pbuf->ill = (uint32_t)(50 + random(10));
	pbuf->bet = (uint32_t)random(100);
	pbuf->adc = (uint32_t)random(220);
	
	
	printf("%d\n",pbuf->snum);
	printf("%d\n",pbuf->temp[0]);
	printf("%d\n",pbuf->hum[0]);
	printf("%d\n",pbuf->x);
	printf("%d\n",pbuf->y);
	printf("%d\n",pbuf->z);	
	printf("%d\n",pbuf->ill);
	printf("%d\n",pbuf->bet);
	printf("%d\n",pbuf->adc);

	send(sockfd,pbuf,sizeof(MSG),0);
}
int main(int argc, char *argv[])
{
	MSG msg;
	int sockfd;
	struct sockaddr_in servaddr;
	srand((int)time(0));

	if(argc < 3)
	{
		printf("Usage : %s <serv_ip> <serv_port>\n", argv[0]);
		exit(-1);
	}
	
	//创建客户端socket
	if ((sockfd = socket(PF_INET, SOCK_STREAM, 0)) < 0)
	{
		perror("fail to socket");
		exit(-1);
	}
	
	//绑定套接字
	bzero(&servaddr, sizeof(servaddr));
	servaddr.sin_family = PF_INET;
	servaddr.sin_port = htons(atoi(argv[2]));
	servaddr.sin_addr.s_addr = inet_addr(argv[1]);
	
	//连接服务器
	if (connect(sockfd, (SA *)&servaddr, sizeof(servaddr)) < 0)
	{
		perror("fail to connect");
		exit(-1);
	}
	
	while(1)
	{
		do_set_info(sockfd,&msg);
		sleep(1);
	}

	return 0;
}
